import {
  getResourceManager
} from "../12-ResourceManager/ResourceManager";
import {
  resourceManager
} from "@kit.LocalizationKit";
import {
  FileManager
} from "./FileManager";

export namespace RawFileService {
  export function isItemDir(path: string): boolean {
    return getResourceManager().isRawDir(path)
  }

  /**
   * 判断是否为文件夹接口如果报错,是文件不存在.
   * @param path
   * @returns
   */
  export function isItemExist(path: string): boolean {
    try {
      getResourceManager().isRawDir(path)
      return true;
    } catch (e) {
      return false;
    }
  }

  /**
   * 返回文件夹的一层数据所有项目
   * 如果dirPath是文件，会报错
   * @param dirPath
   * @param returnFullPath 是否返回全路径.
   * @returns
   */
  export function listFileInDirSync(dirPath: string, returnFullPath: boolean = true): string[] {
    let rm: resourceManager.ResourceManager = getResourceManager();
    let subs = rm.getRawFileListSync(dirPath)
    if (returnFullPath) {
      subs = subs.map(_name => `${dirPath}/${_name}`)
    }
    return subs;
  }

  export function readTextSync(fileName: string): string {
    let byteArray: Uint8Array = readBytesSync(fileName);
    let content = FileManager.uint8ArrayToString(byteArray);
    return content;
  }

  export function readBytesSync(fileName: string): Uint8Array {
    let rm: resourceManager.ResourceManager = getResourceManager();
    let byteArray = rm.getRawFileContentSync(fileName)
    return byteArray;
  }

  export async function readBytes(fileName: string): Promise<Uint8Array> {
    let rm: resourceManager.ResourceManager = getResourceManager();
    let byteArray = await rm.getRawFileContent(fileName)
    return byteArray;
  }


}